#include <iostream>
using namespace std;
#include "brass.h"
#include <cstring>

Brass::Brass(const char *s, long an, double bal) {
  strncpy(fullname, s, MAX - 1);
  fullname[MAX - 1] = '\0';
  acctNum = 0;
  balance = bal;
}

void Brass::Deposit(double amt) {
  if (amt < 0) {
    cout << "无效的数值" << endl;
  } else {
    balance += amt;
  }
}

void Brass::Withdraw(double amt) {
  if (amt < 0) {
    cout << "无效" << endl;
  } else if (amt < balance) {
    balance -= amt;
  } else {
    cout << "没有那么多钱：" << amt << endl;
  }
}

double Brass::Balance() const { return balance; }

// 显示普通用户的账户
void Brass::ViewAcct() const {
  ios_base::fmtflags init = cout.setf(ios_base::fixed, ios_base::floatfield);
  cout.setf(ios_base::showpoint);
  cout.precision(2);
  cout << "Client :" << fullname << endl;
  cout << "Accent Number:" << acctNum << endl;
  cout << "Balance:" << balance << endl;
  cout.setf(init);
}

// 由于派生类不能直接访问基类的私有成员变量，所以通过使用基类的构造函数对私有的成员变量进行初始化。
BrassPlus::BrassPlus(const char *s, long an, double bal, double m1, double r)
    : Brass(s, an, bal) {
  maxLoan = m1;
  rate = r;
  owesBank = 0.0;
}

BrassPlus::BrassPlus(const Brass &ba, double m1, double r) : Brass(ba) {
  maxLoan = m1;
  rate = r;
  owesBank = 0.0;
}

void BrassPlus::ViewAcct() const {
  ios_base::fmtflags init = cout.setf(ios_base::fixed, ios_base::floatfield);
  cout.setf(ios_base::showpoint);
  cout.precision(2);

  Brass::ViewAcct(); // 复用了基类的显示账户的方法
  cout << "Maximum loan:" << maxLoan << endl;
  cout << "Owed to bank:" << owesBank << endl;
  cout << "Loan rate:" << 100 * rate << "%" << endl;
  cout.setf(init);
}

void BrassPlus::Withdraw(double amt) {
  ios_base::fmtflags init = cout.setf(ios_base::fixed, ios_base::floatfield);
  cout.setf(ios_base::showpoint);
  cout.precision(2);

  double bal = Balance();
  if (amt < 0) {
    cout << "无效" << endl;
  } else if (amt <= bal + maxLoan - owesBank) {
    double advance = amt - bal;
    owesBank += advance * (1.0 + rate);
    cout << "Bank advance:" << advance << endl;
    cout << "Finance charge:" << advance * rate << endl;
    Deposit(advance);
    Brass::Withdraw(amt);
  } else {
    cout << "无效" << endl;
  }
  cout.setf(init);
}
